

package com.mt.repair.controller.repairManagement;

import com.mt.common.core.web.JsonResult;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.repair.dto.repairManagement.RepairOrderEditDto;
import com.mt.repair.entity.repairManagement.RepairOrder;
import com.mt.repair.service.repairManagement.RepairOrderService;
import com.mt.common.system.service.UserService;


@Api(tags = "维修单")
@RestController
@RequestMapping("/api/repair/repairManagement/RepairOrder")
@CrossOrigin(allowCredentials = "true")
public class RepairOrderController {
    private static Logger logger = LoggerFactory.getLogger(RepairOrderController.class);


    @Autowired
    private RepairOrderService repairOrderService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询维修单集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("分页查询维修单")
    @ApiPageParam
    @PostMapping("/findRepairOrders")
    public PageResultDTO findRepairOrders(@RequestBody PageDTO pageDTO) {
        return this.repairOrderService.findRepairOrders(pageDTO);
    }

    /**
     * 根据ID查询指定的维修单
     *
     * @param repairOrderId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("根据id查询维修单")
    @ApiPageParam
    @PostMapping("/findRepairOrder")
    public RepairOrder findRepairOrder(@RequestParam Long repairOrderId) {
        return this.repairOrderService.findRepairOrder(repairOrderId);
    }

    /**
     * 根据ID查询指定的维修单(包含外键名称)
     *
     * @param repairOrderId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("根据ID查询指定的维修单(包含外键名称)")
    @PostMapping("/findRepairOrderForView")
    public RepairOrder findRepairOrderForView(@RequestParam Long repairOrderId) {
        return this.repairOrderService.findRepairOrderWithForeignName(repairOrderId);
    }

    /**
     * 根据ID查询指定的维修单(包含维修单和外键名称)
     *
     * @param repairOrderId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("根据ID查询指定的维修单(包含维修单和外键名称)")
    @PostMapping("/findRepairOrderForEdit")
    public RepairOrderEditDto findRepairOrderForEdit(@RequestParam Long repairOrderId) {
        RepairOrderEditDto repairOrderEditDto = new RepairOrderEditDto();
        repairOrderEditDto.setRepairOrder(this.repairOrderService.findRepairOrderWithForeignName(repairOrderId));

        this.prepareRepairOrderEditDto(repairOrderEditDto);

        return repairOrderEditDto;
    }

    /**
     * 根据ID查询指定的维修单(只提取ID 和 Name)
     *
     * @param repairOrderId Id
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("根据ID查询指定的维修单(只提取ID 和 Name)")
    @PostMapping("/findRepairOrdersWithIdNameById")
    public RepairOrder findRepairOrdersWithIdNameById(@RequestParam Long repairOrderId) {
        return this.repairOrderService.findRepairOrdersWithIdNameById(repairOrderId);
    }

    /**
     * 根据名称查询维修单集合(只提取ID 和 Name)
     *
     * @param repairOrderName 名称
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:view')")
    @ApiOperation("根据名称查询维修单集合(只提取ID 和 Name)")
    @PostMapping("/findRepairOrdersWithIdNameByName")
    public List<RepairOrder> findRepairOrdersWithIdNameByName(@RequestParam String repairOrderName) {
        return this.repairOrderService.findRepairOrdersWithIdNameByName(repairOrderName);
    }


    /**
     * 创建新的维修单
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:add')")
    @ApiOperation("创建新的维修单")
    @PostMapping("/createRepairOrder")
    public RepairOrderEditDto createRepairOrder() {
        RepairOrderEditDto repairOrderEditDto = new RepairOrderEditDto();
        repairOrderEditDto.setRepairOrder(new RepairOrder());

        this.prepareRepairOrderEditDto(repairOrderEditDto);
        return repairOrderEditDto;
    }

    private void prepareRepairOrderEditDto(RepairOrderEditDto repairOrderEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        repairOrderEditDto.setRepairApplicantUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存维修单
     *
     * @param repairOrder 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:add')")
    @ApiOperation("新增保存维修单")
    @PostMapping("/saveRepairOrder")
    public RepairOrder saveRepairOrder(@RequestBody RepairOrder repairOrder) {
        return this.repairOrderService.saveRepairOrder(repairOrder);
    }

    /**
     * 修改保存维修单
     *
     * @param repairOrder 实体对象
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:update')")
    @ApiOperation("修改保存维修单")
    @PostMapping("/updateRepairOrder")
    public RepairOrder updateRepairOrder(@RequestBody RepairOrder repairOrder) {
        return this.repairOrderService.updateRepairOrder(repairOrder);
    }

    /**
     * 根据ID删除维修单
     *
     * @param repairOrderId ID
     */
    @PreAuthorize("hasAuthority('repair:repairManagement:RepairOrder:remove')")
    @ApiOperation("根据ID删除维修单")
    @PostMapping("/deleteRepairOrder")
    public void deleteRepairOrder(@RequestParam Long repairOrderId) {
        this.repairOrderService.deleteRepairOrder(repairOrderId);
    }

    @ApiOperation("派单")
    @PostMapping("/dispatch")
    public JsonResult dispatch(@RequestParam Long userId, @RequestParam Long repairOrderId) {
        return this.repairOrderService.dispatch(userId, repairOrderId);
    }
	@ApiOperation("更新订单、用于维修工开始维修、完成维修")
	@PostMapping("/updateStatus")
	public JsonResult updateStatus(@RequestParam Integer currentState, @RequestParam String status,@RequestParam String setTimeName,@RequestParam Long repairOrderId) {
		return this.repairOrderService.updateStatus(currentState, status,setTimeName,repairOrderId);
	}

    @ApiOperation("开始维修")
    @PostMapping("/startRepair")
    public JsonResult startRepair(@RequestParam Long repairOrderId) {
        return this.repairOrderService.startRepair(repairOrderId);
    }
    @ApiOperation("完成维修")
    @PostMapping("/endRepair")
    public JsonResult endRepair(@RequestParam Long repairOrderId) {
        return this.repairOrderService.endRepair(repairOrderId);
    }
    @ApiOperation("统计近七天的每日订单数量")
    @PostMapping("/countOrders")
    public JsonResult countOrders(@RequestParam(required = false)Integer day) {
        return this.repairOrderService.countOrders(day);
    }

    @ApiOperation("统计近七天的报修设备类型统计")
    @PostMapping("/countRepairDeviceType")
    public JsonResult countRepairDeviceType(@RequestParam(required = false)Integer day) {
        return this.repairOrderService.countRepairDeviceType(day);
    }

    @ApiOperation("统计近七天的某设备类型的故障类型统计")
    @PostMapping("/countRepairFaultTypeByDeviceType")
    public JsonResult countRepairFaultTypeByDeviceType(@RequestParam(required = false)Integer day,@RequestParam(required = false)String deviceType) {
        return this.repairOrderService.countRepairFaultTypeByDeviceType(day,deviceType);
    }

    @ApiOperation("工单催单")
    @PostMapping("/reminder")
    public JsonResult reminder(@RequestBody RepairOrder repairOrder) {
        return this.repairOrderService.reminder(repairOrder);
    }

    @ApiOperation("工单退单")
    @PostMapping("/chargeback")
    public JsonResult chargeback(@RequestParam Long repairOrderId,@RequestParam String reason) {
        return this.repairOrderService.chargeback(repairOrderId,reason);
    }

    @ApiOperation("工单取消")
    @PostMapping("/cancelRepair")
    public JsonResult cancelRepair(@RequestParam Long repairOrderId) {
        return this.repairOrderService.cancelRepair(repairOrderId);
    }
}

